# 京东商城购物车信息爬取
import requests
from requests.exceptions import RequestException
from bs4 import BeautifulSoup

# 定义headers头信息及cookie信息
headers = {
    'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/'
              'webp,image/apng,*/*;q=0.8',
    'accept-encoding': 'deflate, br',
    'accept-language': 'zh-CN,zh;q=0.9,zh-TW;q=0.8,fr;q=0.7,en;q=0.6',
    'cache-control': 'max-age=0',
    'cookie': '__jdu=1498206290505398198425; shshshfpa=a02e1472-2cff-c6aa-b'
              'ad2-7aaaebb0224f-1528095497; shshshfpb=03015a6a1840340e8967e9'
              '2d29125c71baac2f4a058af84b75b14e309c; unpl=V2_ZzNtbRBUEBQiCUNT'
              'KE5fUGIHFwlKVEQcJw0WUS8fXwRgBUBdclRCFXwUR11nGlwUZwEZXkBcQhNFCH'
              'ZXchBYAWcCGllyBBNNIEwHDCRSBUE3XHxcFVUWF3RaTwEoSVoAYwtBDkZUFBY'
              'hW0IAKElVVTUFR21yVEMldQl2VH4RVQZmBBFVQ1NCEXcAT1RzEFkGYzMiWnJn'
              'cx12DEFTSxhsBFdBRgFFVEMUdwoLVH4RVQZmBBFVQ1NCEXcAT1RzEFkGYzMTb'
              'UE%3d; __jda=122270672.1498206290505398198425.1498206290.1531'
              '730040.1534828988.5; __jdc=122270672; __jdv=122270672|baidu-'
              'pinzhuan|t_288551095_baidupinzhuan|cpc|0f3d30c8dba7459bb52f2e'
              'b5eba8ac7d_0_c2b0f046bf2d454e9268c4a4e72067c1|1534828988231; P'
              'CSYCityID=1; user-key=81520196-ba62-4e70-9fa7-6855ff185f87; 3'
              'AB9D23F7A4B3C9B=RD4QC5HPCL6YLIVWGUUMDB7HXTRQU5QJEKMBZYSIY4IOE'
              'NXQSZP6WOVUFI2LSDWCQ7UABNT37YR3VCZOSX77ZYPGB4; ipLoc-djd=1-72'
              '-2819; cart-main=xx; cn=3; cd=0; shshshfp=5ba238a6d1e60e1642bf'
              '8a720d9e543e; shshshsID=27b32d7d458166b462378e98baeb91f2_6_153'
              '4829537571; __jdb=122270672.13.1498206290505398198425|5.153482'
              '8988',
    'referer': 'https://www.jd.com/?cu=true&utm_source=baidu-pinzhuan&utm_med'
               'ium=cpc&utm_campaign=t_288551095_baidupinzhuan&utm_term=0f3d3'
               '0c8dba7459bb52f2eb5eba8ac7d_0_c2b0f046bf2d454e9268c4a4e72067c1',
    'upgrade-insecure-requests': '1',
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKi'
                  't/537.36 (KHTML, like Gecko) Chrome/68.0.3440.1'
                  '06 Safari/537.36',
}

# 京东商城购物车网址
url = 'https://cart.jd.com/cart.action'

try:
    # 执行爬取
    res = requests.get(url=url, headers=headers)
except RequestException as e:
    print(e)
    res = None

if res.status_code == 200:
    # 使用BeautifulSoup解析内容
    soup = BeautifulSoup(res.text, 'lxml')
else:
    print('打开网址失败')
    soup = None

if soup:
    # print(soup.prettify())
    # with open('./myhtml.html', 'w', encoding='utf-8') as f:
    #     f.write(soup.prettify())
    slist = soup.find_all('div', attrs={"class": "cart-tbody"})
    for shop in slist:
        shopname = shop.find('div', class_='p-name').find(
            'a').get_text().strip()
        print('商品名：' + shopname)
        shopprice = shop.find('div', class_='p-price').find(
                'strong').get_text().strip()
        print('单价：', shopprice)
        shopquantity = \
            shop.find('div', class_='quantity-form').find('input').attrs[
                'value']
        print('购买数量：', shopquantity)
        shoptprice = shop.find('div', class_='p-sum').find(
                'strong').get_text().strip()
        print('总价：', shoptprice)
        print('=' * 60)
